#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

char message[100];

void error_handling(char *message) {
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

int main(int argc, char *argv[]) {
    int client_sock;
    if (argc != 3) {
        printf("Usage : %s <IP> <port>\n", argv[0]);
        exit(1);
    }
    if ((client_sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        error_handling("socket() error!");
    }
    
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(argv[1]);
    server_addr.sin_port = htons(atoi(argv[2]));

    if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        error_handling("connect() error!");
    }

 

    //sleep(1);
    int ret = read(client_sock, message, 30);
    //printf("\n");0
    printf("ret %d, len = %d\n", ret, strlen(message));
    printf("Message from server : %s \n", message);
//  //   close(client_sock);
//     ret = read(client_sock, message, 10);
//     //printf("\n");
//     printf("ret %d, len = %d\n", ret, strlen(message));
//     printf("Message from server : %s \n", message);
   for (int i = 0; i < ret; i++)
       printf("%c", message[i]);
    close(client_sock);
    return 0;
}